<?php

namespace App\Http\Controllers\Admin;

use App\Models\Classes;
use App\Models\Teacher;
use App\Models\LevelTeacher;
use App\Models\Type;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

/**
 * 用户分类管理
 * Class TypeController
 * @package App\Http\Controllers\Admin
 */
class TeacherController extends BaseController
{
    protected $is_logistics = 0; //是否后勤
    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $logistics = $this->is_logistics;
        $query = $request->only(['name', 'tl_code', 'tc_code']);

        $level = LevelTeacher::all();

        $where = Teacher::selectQuery($query);
        $list = Teacher::where($where)->where('is_logistics', $this->is_logistics)->paginate(config('site.other.paginate'));
        $list = $list->appends($request->input());
        return backendView('teacher.index', compact('list', 'level', 'query', 'logistics'));
    }

    /**
     * 新增分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'tc_code' => 'required|max:255|unique:teachers,tc_code',
                'name' => 'required|max:255',
                'phone' => 'required|mobile',
                'tl_code' => 'required',
            ], ['phone.mobile' => '手机号码不正确'], [
                'tc_code' => __("code"),
                'name' => __("Name"),
                'phone' => '手机号码',
                'tl_code' => __("Level"),
            ]);

            $data = $request->only(['tc_code','name','tl_code','phone']);
            $data['is_logistics'] = $this->is_logistics;
            $route = $this->is_logistics ? 'admin.logistics.index' : 'admin.teacher.index';
            if (Teacher::add($data)) {
                return redirect()->route($route)->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            $level = LevelTeacher::all();
            $logistics = $this->is_logistics;
            return backendView('teacher.add',compact('level','logistics'));
        }
    }

    /**
     * 编辑分类
     * @param Request $request
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request, $id = null)
    {

        $teacher = Teacher::find($id);
        if (empty($teacher)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }
        $levelIds = [];
        foreach ($teacher->levels as $level) {
            $levelIds[] = $level->tl_code;
        }
        $level = LevelTeacher::all();
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'tc_code' => ['required', 'max:255',Rule::unique('teachers')->ignore($teacher->id)],
                'name' => 'required|max:255',
                'phone' => 'required|mobile',
                'tl_code' => 'required',
            ], ['phone.mobile' => '手机号码不正确'], [
                'tc_code' => __("code"),
                'name' => __("Name"),
                'phone' => '手机号码',
                'tl_code' => __("Level"),
            ]);

            $data = $request->only(['tc_code','name','tl_code', 'phone']);
            $route = $this->is_logistics ? 'admin.logistics.index' : 'admin.teacher.index';
            if ($teacher->edit($data)) {
                return redirect()->route($route)->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            $logistics = $this->is_logistics;
            return backendView('teacher.edit', compact('teacher','level', 'levelIds', 'logistics'));
        }
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function view($id = null)
    {
        $teacher = Teacher::find($id);
        $logistics = $this->is_logistics;
        if (empty($teacher)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        return backendView('teacher.view', compact('teacher', 'logistics'));
    }

    /**
     * 删除分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        $teacher = Teacher::find($id);
        if (empty($teacher)) {
            return back()->with('error', __('The page you requested was not found'));
        }
        $class = Classes::where('tc_code', $teacher->tc_code)->find();
        if (empty($class)) {
            return back()->with('error', '请先解除与'.$class->name.'的班主任关系');
        }

        try {
            if ($teacher->delete()) {
                $teacher->levels()->detach($teacher->levels);
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }
}